package org.jetnuke.modules.download;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.DownloadDownloadDAO;
import org.jetnuke.entities.DownloadsDownloads;
import org.jetnuke.entities.DownloadsVoteData;

public class Details implements IAction {

	int id;

	public void setId(int id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		DownloadDownloadDAO ddm=DataAccessDriver.getInst().newDownloadDownloadDAO();
		
		DownloadsDownloads down = ddm.retrieveByPK(id);
		vc.put("down", down);
		List ls = DataAccessDriver.getInst().newDownloadVoteDAO().getByRatingLid(id);
			//downloads_votedataPeer.doSelect(new Criteria().add(
				//downloads_votedataPeer.RATINGLID, id));
		Iterator it = ls.iterator();
		HashMap regVoteRating=new HashMap();
		HashMap anonVoteRating=new HashMap();
		
		HashMap rvvchartheight=new HashMap();
		HashMap avvchartheight=new HashMap();
		
		HashMap rvvpercent=new HashMap();
		HashMap avvpercent=new HashMap();
		
		
		Integer zero=new Integer(0);
		
		for(int i=1;i<=10;i++){
			Integer kk=new Integer(i);
			regVoteRating.put(kk,zero);
			anonVoteRating.put(kk,zero);
		}
		
		int totalVotes=0;
		int totalRegVotes=0;
		int totalAnonVotes=0;
		int topReg=0;
		int bottomReg=10;
		int totalRegSum=0;
		
		int topAnon=0;
		int bottomAnon=10;
		int totalAnonSum=0;
		
		while (it.hasNext()) {
			totalVotes++;
			DownloadsVoteData vote=(DownloadsVoteData)it.next();
			String user=vote.getRATINGUSER();
			int voteRating=vote.getRATING();
			if(voteRating<1)
				voteRating=1;
			if(voteRating>10)
				voteRating=10;
			
			if(user!=null && user.length()>0){
				totalRegVotes++;
				totalRegSum+=voteRating;
				Integer kk=new Integer(voteRating);
				Integer count=(Integer)(regVoteRating.get(kk));
				regVoteRating.put(kk,new Integer(count.intValue()+1));
				
			}else{
				totalAnonVotes++;
				totalAnonSum+=voteRating;
				Integer kk=new Integer(voteRating);
				Integer count=(Integer)(anonVoteRating.get(kk));
				anonVoteRating.put(kk,new Integer(count.intValue()+1));
			}
		}
		if(totalRegVotes>0){
			for(int i=1;i<=10;i++){
				Integer kk=new Integer(i);
				int count=((Integer)regVoteRating.get(kk)).intValue();
				if(count>0 && i>topReg)
					topReg=i;
				if(count>0 && i<bottomReg  )
					bottomReg=i;
				rvvchartheight.put(kk,new Integer((int)(count*100./totalRegVotes)));
				rvvpercent.put(kk,new Integer((int)(count*100./totalRegVotes)));
			}
		}
		if(totalAnonVotes>0){
			for(int i=1;i<=10;i++){
				Integer kk=new Integer(i);
				int count=((Integer)anonVoteRating.get(kk)).intValue();
				if(count>0 && i>topAnon)
					topAnon=i;
				if(count>0 && i<bottomAnon )
					bottomAnon=i;
				
				avvchartheight.put(kk,new Integer((int)(count*100./totalAnonVotes)));
				avvpercent.put(kk,new Integer((int)(count*100./totalAnonVotes)));
			}
		}
		vc.put("totalVotes", new Integer(totalVotes));
		vc.put("totalRegVotes", new Integer(totalRegVotes));
		vc.put("totalAnonVotes", new Integer(totalAnonVotes));
		vc.put("rvv",regVoteRating);
		vc.put("rvvchartheight",rvvchartheight);
		vc.put("rvvpercent",rvvpercent);
		vc.put("avv",anonVoteRating);
		vc.put("avvchartheight",avvchartheight);
		vc.put("avvpercent",avvpercent);
		if(totalRegVotes>0){
			vc.put("topreg",new Integer(topReg));
			vc.put("bottomreg",new Integer(bottomReg));
			vc.put("avgRU",new Double(totalRegSum*1.0/totalRegVotes));
		}else{
			vc.put("topreg","");
			vc.put("bottomreg","");
			vc.put("avgRU","");
		}
		if(totalAnonVotes>0){
			vc.put("topanon",new Integer(topAnon));
			vc.put("bottomanon",new Integer(bottomAnon));
			vc.put("avgAU",new Double(totalAnonSum*1.0/totalAnonVotes));
		}else{
			vc.put("topanon","");
			vc.put("bottomanon","");
			vc.put("avgAU","");
		}
		vc.put("anonweight","10");//TODO the same value used in Rate
		vc.put("finalrating",new Double(down.getDOWNLOADRATINGSUMMARY()));
		return new ActionForward(op, info);
	}

}
